package com.define.ssh.dao.impl;


import java.util.HashMap;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.define.ssh.dao.GroupinfoRegularDao;
import com.define.ssh.model.GroupinfoRegular;
import com.define.ssh.model.GroupmemberRegular;
import com.define.ssh.util.Pager;

@Repository("groupinfoRegularDaoImpl")
public class GroupinfoRegularDaoImpl extends BaseDaoImpl<GroupinfoRegular, Long> implements GroupinfoRegularDao {
	
	public Pager findPagerByGroupinfoRegular(Pager pager, HashMap<String, Object> hm) {
		final Criteria criteria = getSession().createCriteria(GroupmemberRegular.class);

		if (hm.get("name") != null) {
			criteria.createAlias("userInfo", "userInfo");
		}
		/*
		if (hm.get("deptids") != null) {
			// criteria.add(Restrictions.eq("deptid", hm.get("deptid")));

			final String ds = hm.get("deptids").toString();
			System.out.println("=========================" + ds);
			final String str[] = ds.split(",");
			final Integer array[] = new Integer[str.length];
			for (Integer i = 0; i < str.length; i++) {
				array[i] = Integer.parseInt(str[i]);
			}
			criteria.add(Restrictions.in("deptid", array));
		} else {
			criteria.add(Restrictions.eq("deptid", 0));
		}
		if (hm.get("user_type") != null
				&& StringUtils.hasText(hm.get("user_type").toString())) {
			criteria.add(Restrictions.eq("userInfo.user_type",
					Integer.valueOf(hm.get("user_type").toString())));
		}*/
		if (hm.get("groupid") != null
				&& StringUtils.hasText(hm.get("groupid").toString())) {
			criteria.add(Restrictions.eq("groupid",hm.get("groupid").toString()));
		}
		criteria.add(Restrictions.ne("updatetype",3));
		if (hm.get("name") != null
				&& StringUtils.hasText(hm.get("name").toString())
				&& !"输入姓名、工号等进行查询".equals(hm.get("name").toString())) {
			// criteria.createAlias("userInfo", "userInfo");
//			criteria.add(Restrictions.like("userInfo.name", "%"
//					+ hm.get("name").toString() + "%"));
			 criteria.add(Restrictions.or(Restrictions.like("userInfo.name",
			 "%" + hm.get("name").toString() + "%"),
			 Restrictions.like("userInfo.userCode", "%" +
			 hm.get("name").toString() + "%")));

		}

		criteria.addOrder(Order.desc("updateTime"));
		// pager.setSearchBy("");
		pager = findPager(pager, criteria);
		/*
		 * pager.setSearchBy(searchBy); pager.setKeyword(keyword);
		 */
		return pager;
	}
}
